package cn.hznc.controller.inventory;

import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.controller.service.inventory.ToolPartsInventoryService;
import cn.hznc.domain.entity.LocationImportEntity;
import cn.hznc.domain.entity.common.WarehouseImportEntity;
import cn.hznc.domain.request.inventory.*;
import cn.hznc.domain.request.schedule.ToolReturnIndexReq;
import cn.hznc.domain.request.schedule.ToolReturnReq;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.dto.FirstStockImport;
import cn.hznc.dto.IToolCabinetStockImport;
import cn.hznc.dto.ToolConnectImport;
import cn.hznc.dto.ToolConnectSimpleImport;
import cn.hznc.excel.*;
import cn.hznc.exception.ShelvesException;
import cn.hznc.exception.SystemException;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.controller.service.inventory.InventoryLocationService;
import cn.hznc.utils.FileUploadUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.read.metadata.ReadSheet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/4/2 15:24
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/inventoryLocation")
public class InventoryLocationController {

    @Autowired
    private InventoryLocationService inventoryLocationService;

    @Autowired
    private ToolPartsInventoryService toolPartsInventoryService;

    /**
     * 根据仓库类型获取货架列表
     *
     * @param queryWarehouseReq
     * @return
     */
    @RequestMapping(value = "/getWarehouseList", method = RequestMethod.POST)
    public ResponseBody getWarehouseList(@RequestBody QueryWarehouseReq queryWarehouseReq) {
        return ResponseBody.success(inventoryLocationService.getWarehouseList(queryWarehouseReq));
    }

    /**
     * 编辑仓库安全库存配置
     *
     * @param warehouseSafetyInventoryParamReq
     * @return
     */
    @RequestMapping(value = "/editSafetyInventoryConfigure", method = RequestMethod.POST)
    @Log(title = "编辑仓库安全库存配置" , businessType = BusinessType.UPDATE)
    public ResponseBody editSafetyInventoryConfigure(@RequestBody WarehouseSafetyInventoryParamReq warehouseSafetyInventoryParamReq) {
        return ResponseBody.success(inventoryLocationService.editSafetyInventoryConfigure(warehouseSafetyInventoryParamReq));
    }

    /**
     * 查询仓库安全库存配置
     *
     * @return
     */
    @RequestMapping(value = "/qrySafetyInventoryConfigure", method = RequestMethod.GET)
    public ResponseBody qrySafetyInventoryConfigure(@RequestParam("id") Integer id,
                                                    @RequestParam(value = "toolNo" , required = false) String toolNo,
                                                    @RequestParam(value = "partno" , required = false) String partno,
                                                    @RequestParam("flag") Integer flag) {
        return ResponseBody.success(inventoryLocationService.qrySafetyInventoryConfigure(id , toolNo ,partno , flag));
    }

    /**
     * 根据货架获取库位列表
     *
     * @param warehouseId
     * @return
     */
    @RequestMapping(value = "/getLocationList/{warehouseId}", method = RequestMethod.GET)
    public ResponseBody getLocationList(@PathVariable Integer warehouseId) {
        return ResponseBody.success(inventoryLocationService.getLocationList(warehouseId));
    }

    /**
     * 单个库位获取工件简单列表
     *
     * @param inventoryLocationId
     * @return
     */
    @RequestMapping(value = "/getLocationChildList/{inventoryLocationId}/{warehouseType}", method = RequestMethod.GET)
    public ResponseBody getLocationChildList(@PathVariable Integer inventoryLocationId,
                                             @PathVariable String warehouseType) {
        return ResponseBody.success(inventoryLocationService.getLocationChildList(inventoryLocationId, warehouseType));
    }

    /**
     * 新增或编辑库位
     *
     * @param addOrEditLocationReq
     * @return
     */
    @RequestMapping(value = "/addOrEditLocation", method = RequestMethod.POST)
    @Log(title = "新增或编辑库位" , businessType = BusinessType.OTHER)
    public ResponseBody addOrEditLocation(@RequestBody AddOrEditLocationReq addOrEditLocationReq) {
        try {
            inventoryLocationService.addOrEditLocation(addOrEditLocationReq);
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 新增或编辑智能柜库位
     *
     * @return
     */
    @RequestMapping(value = "/addOrEditSmartLocation", method = RequestMethod.POST)
    @Log(title = "新增或编辑智能柜库位" , businessType = BusinessType.OTHER)
    public ResponseBody addOrEditSmartLocation(@RequestBody AddOrEditSmartLocationReq addOrEditSmartLocationReq) {
        try {
            inventoryLocationService.addOrEditSmartLocation(addOrEditSmartLocationReq);
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 单个库位入库零件
     *
     * @return
     */
    @RequestMapping(value = "/inboundLocationTool", method = RequestMethod.POST)
    public ResponseBody InboundLocationTool(@RequestBody InBoundOrOutBoundReq inBoundOrOutBoundReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundLocationTool(inBoundOrOutBoundReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位入库零件（不选择库位 选择货架）
     *
     * @return
     */
    @RequestMapping(value = "/InboundLocationToolReload", method = RequestMethod.POST)
    public ResponseBody InboundLocationToolReload(@RequestBody InBoundOrOutBoundReloadReq inBoundOrOutBoundReloadReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundLocationToolReload(inBoundOrOutBoundReloadReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }


    /**
     * 条件查询库位
     *
     * @return
     */
    @RequestMapping(value = "/queryAllLocation", method = RequestMethod.GET)
    public ResponseBody queryLocation(@RequestParam("warehouseType") String warehouseType, @RequestParam("warehouseId") String warehouseId, @RequestParam("locationNum") String locationNum, @RequestParam("toolNo") String toolNo) {
        return ResponseBody.success(inventoryLocationService.queryAllLocation(warehouseType, warehouseId, locationNum, toolNo));
    }

    /**
     * 库存总成类型创建
     *
     * @return
     */
    @RequestMapping(value = "/createAssemblyTool", method = RequestMethod.POST)
    @Log(title = "新增库存总成类型" , businessType = BusinessType.INSERT)
    public ResponseBody createAssemblyTool(@RequestBody CreateAssemblyToolReq createAssemblyToolReq) {
        try {
            return ResponseBody.success(inventoryLocationService.createAssemblyTool(createAssemblyToolReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 根据刀号类型查询库位
     *
     * @return
     */
    @RequestMapping(value = "/queryLocationByToolNo", method = RequestMethod.GET)
    public ResponseBody queryLocationByToolNo(@RequestParam("toolNo") String toolNo,
                                              @RequestParam(required = false ,value = "toolType")String toolType,
                                              @RequestParam(required = false , value = "shankType")String shankType) {
        try {
            return ResponseBody.success(inventoryLocationService.queryLocationByToolNo(toolNo , toolType , shankType));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }


    /**
     * 单个库位入库总成（根据最近一次出库刀片记录入库）
     *
     * @return
     */
    @RequestMapping(value = "/inboundLocationAssemblyTool", method = RequestMethod.POST)
    public ResponseBody InboundLocationAssemblyTool(@RequestBody InBoundOrOutBoundAssemblyReq inBoundOrOutBoundAssemblyReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundLocationAssemblyTool(inBoundOrOutBoundAssemblyReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位入库总成（用户选择刀片库位以及刀柄库位）
     *
     * @return
     */
    @RequestMapping(value = "/inboundToolPartAssemblyTool", method = RequestMethod.POST)
    public ResponseBody inboundToolPartAssemblyTool(@RequestBody InboundToolPartAssemblyToolReq inboundToolPartAssemblyToolReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundToolPartAssemblyTool(inboundToolPartAssemblyToolReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位入库总成（接收两个零件rfid）
     *
     * @return
     */
    @RequestMapping(value = "/inboundToolPartAssemblyToolReload", method = RequestMethod.POST)
    public ResponseBody inboundToolPartAssemblyToolReload(@RequestBody InboundToolPartAssemblyToolReloadReq inboundToolPartAssemblyToolReloadReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundToolPartAssemblyToolReload(inboundToolPartAssemblyToolReloadReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位唯一编号入库总成
     *
     * @return
     */
    @RequestMapping(value = "/inboundUniqueAssemblyTool", method = RequestMethod.POST)
    public ResponseBody inboundUniqueAssemblyTool(@RequestBody InboundAssemblyReq inboundAssemblyReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundUniqueAssemblyTool(inboundAssemblyReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位唯一编号数组入库零件
     *
     * @return
     */
    @RequestMapping(value = "/inboundBatchUniqueTool", method = RequestMethod.POST)
    public ResponseBody inboundBatchUniqueTool(@RequestBody InboundToolReq inboundToolReq) {
        try {
            return ResponseBody.success(inventoryLocationService.inboundUniqueTool(inboundToolReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位入库总成图片上传
     *
     * @return
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResponseBody
    uploadFile(@RequestPart("file") MultipartFile file,
                                   @RequestPart("toolNo") String toolNo,@RequestPart("flag")String flag) {
        //String flag = "2";
        return ResponseBody.success(inventoryLocationService.uploadFile(file, toolNo , flag));
    }

    /**
     * 单个库位出库零件
     *
     * @return
     */
    @RequestMapping(value = "/outboundLocationTool", method = RequestMethod.POST)
    public ResponseBody outboundLocationTool(@RequestBody InBoundOrOutBoundReq inBoundOrOutBoundReq) {
        try {
            return ResponseBody.success(inventoryLocationService.outboundLocationTool(inBoundOrOutBoundReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位出库总成
     *
     * @return
     */
    @RequestMapping(value = "/outboundLocationAssemblyTool", method = RequestMethod.POST)
    public ResponseBody outboundLocationAssemblyTool(@RequestBody OutBoundAssemblyReq outBoundAssemblyReq) {
        try {
            return ResponseBody.success(inventoryLocationService.outboundLocationAssemblyTool(outBoundAssemblyReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 单个库位唯一id出库总成
     *
     * @return
     */
    @RequestMapping(value = "/outboundUniqueAssemblyTool", method = RequestMethod.POST)
    public ResponseBody outboundUniqueAssemblyTool(@RequestBody OutboundUniqueAssemblyReq outboundUniqueAssemblyReq) {
        try {
            return ResponseBody.success(inventoryLocationService.outboundUniqueAssemblyTool(outboundUniqueAssemblyReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }



    /**
     * 单个库位的详细工件信息列表
     *
     * @return
     */
    @RequestMapping(value = "/getLocationToolList", method = RequestMethod.POST)
    public ResponseBody getLocationToolList(@RequestBody LocationToolReq locationToolReq) {
        return ResponseBody.success(inventoryLocationService.getLocationToolList(locationToolReq));
    }

    /**
     * 库位使用日志列表
     *
     * @return
     */
    @RequestMapping(value = "/getLocationUsageLog", method = RequestMethod.POST)
    public ResponseBody getLocationUsageLog(@RequestBody LocationUsageReq locationUsageReq) {
        return ResponseBody.success(inventoryLocationService.getLocationUsageLog(locationUsageReq));
    }


    /**
     * 条件查询系列工件详细信息
     *
     * @return
     */
    @RequestMapping(value = "/getLocationToolDetail", method = RequestMethod.POST)
    public ResponseBody getLocationToolDetail(@RequestBody LocationToolDetailReq locationToolDetailReq) {
        try {
            return ResponseBody.success(inventoryLocationService.getLocationToolDetail(locationToolDetailReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 删除货架接口
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteShelves", method = RequestMethod.GET)
    @Log(title = "删除货架" , businessType = BusinessType.DELETE)
    public ResponseBody deleteShelves(@RequestParam("id") Integer id) {
        try {
            inventoryLocationService.deleteShelves(id);
            return ResponseBody.success();
        } catch (ShelvesException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }


    /**
     * 新增或修改货架接口
     *
     * @param addShelvesReq
     * @return
     */
    @RequestMapping(value = "/addOrupdateShelves", method = RequestMethod.POST)
    @Log(title = "新增或修改货架" , businessType = BusinessType.OTHER)
    public ResponseBody addOrupdateShelves(@RequestBody AddShelvesReq addShelvesReq) {

        try {
            inventoryLocationService.addShelves(addShelvesReq);
            return ResponseBody.success();
        } catch (ShelvesException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 刀具关系表导入接口
     *
     * @return
     */
    @RequestMapping(value = "/importToolConnect", method = RequestMethod.POST)
    @Log(title = "导入刀具号和零件类型关联表" , businessType = BusinessType.OTHER)
    public ResponseBody importToolConnect(@RequestPart("file") MultipartFile file) {
        ToolConnectListener toolConnectListener = new ToolConnectListener();
        ToolConnectSimpleListener toolConnectSimpleListener = new ToolConnectSimpleListener();
        try {
            ExcelReader excelReader = EasyExcel.read(file.getInputStream()).autoCloseStream(true).build();
            FileUploadUtil.checkCommonFile(file);
            ReadSheet toolConnectSheet = EasyExcel.readSheet(0).head(ToolConnectImport.class).headRowNumber(2).registerReadListener(toolConnectListener).build();
            ReadSheet toolConnectSheet1 = EasyExcel.readSheet(1).head(ToolConnectSimpleImport.class).headRowNumber(2).registerReadListener(toolConnectSimpleListener).build();
            excelReader.read(toolConnectSheet, toolConnectSheet1);
            inventoryLocationService.importData(toolConnectListener.getImportData(), toolConnectSimpleListener.getImportData());
            excelReader.finish();
        } catch (SystemException e) {
            log.error("importOrder error========》msg：{}", e.getMsg());
            return ResponseBody.error(e.getCode(), e.getMsg());
        } catch (IOException e) {
            log.error("importOrder error========》msg：{}" + e);
        }
        return ResponseBody.success();
    }

    /**
     * 导入智能柜库位信息
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/importIToolCabinetStock", method = RequestMethod.POST)
    //@Log(title = "导入智能柜库存信息" , businessType = BusinessType.OTHER)
    public ResponseBody importIToolCabinetStock(@RequestPart("file") MultipartFile file , @RequestPart("operator")String operator) {
        try {
            IToolCabinetStockReadListener iToolCabinetStockReadListener = new IToolCabinetStockReadListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), IToolCabinetStockImport.class, iToolCabinetStockReadListener).autoCloseStream(true).sheet().doRead();
            return ResponseBody.success( inventoryLocationService.importIToolCabinetStock(iToolCabinetStockReadListener.getImportData() , operator));
        } catch (IOException ex) {
            log.error("importOrder error========》", ex);
        } catch (SystemException systemException) {
            log.error("importOrder error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 修改智能柜物料编码和描述
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/importIToolCabinetStockReload", method = RequestMethod.POST)
    //@Log(title = "修改智能柜物料编码和描述" , businessType = BusinessType.OTHER)
    public ResponseBody importIToolCabinetStockReload(@RequestPart("file") MultipartFile file) {
        try {
            IToolCabinetStockReadListener iToolCabinetStockReadListener = new IToolCabinetStockReadListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), IToolCabinetStockImport.class, iToolCabinetStockReadListener).autoCloseStream(true).sheet().doRead();
            return ResponseBody.success( inventoryLocationService.importIToolCabinetStockReload(iToolCabinetStockReadListener.getImportData()));
        } catch (IOException ex) {
            log.error("importOrder error========》", ex);
        } catch (SystemException systemException) {
            log.error("importOrder error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    @RequestMapping(value = "/firstStockImport", method = RequestMethod.POST)
    @Log(title = "导入一级库库存信息" , businessType = BusinessType.OTHER)
    public ResponseBody firstStockImport(@RequestPart("file") MultipartFile file){
        try {
            FirstStockReadListener firstStockReadListener = new FirstStockReadListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), FirstStockImport.class, firstStockReadListener).autoCloseStream(true).sheet().doRead();
            return ResponseBody.success( inventoryLocationService.firstStockImport(firstStockReadListener.getImportData()));
        } catch (IOException ex) {
            log.error("importOrder error========》", ex);
        } catch (SystemException systemException) {
            log.error("importOrder error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }


    @RequestMapping(value = "/addSafetyCapacity", method = RequestMethod.POST)
    public ResponseBody addsafetyCapacity(@RequestBody AddSafetyCapacityReq addSafetyCapacityReq) {
        inventoryLocationService.addSafetyCapacity(addSafetyCapacityReq);
        return ResponseBody.success();
    }

    @RequestMapping(value = "/showSafetyCapacity", method = RequestMethod.POST)
    public ResponseBody showSafetyCapacity(@RequestBody AddSafetyCapacityReq addSafetyCapacityReq) {
        return ResponseBody.success(inventoryLocationService.showSafetyCapacity(addSafetyCapacityReq));
    }

    @RequestMapping(value = "/showInventoryMessage", method = RequestMethod.GET)
    public ResponseBody showSafetyCapacity(@RequestParam Integer warehouseId , @RequestParam(value = "flag" , required = false)Integer flag) {
        return ResponseBody.success(inventoryLocationService.showInventoryLocationMessage(warehouseId , flag));
    }

    @RequestMapping(value = "/selectInventoryMessage", method = RequestMethod.GET)
    public ResponseBody selectInventoryMessage(@RequestParam Integer warehouseId, String selectMessage) {
        return ResponseBody.success(inventoryLocationService.selectInventoryMessage(warehouseId, selectMessage));
    }

    @RequestMapping(value = "/delectInventoryLocation", method = RequestMethod.GET)
    @Log(title = "删除仓库中的所有库位" , businessType = BusinessType.DELETE)
    public ResponseBody delectInventoryLocation(@RequestParam Integer warehouseId, String delectInventoryLocation) {
        inventoryLocationService.delectInventoryLocation(warehouseId, delectInventoryLocation);
        return ResponseBody.success();
    }

    @RequestMapping(value = "/addMachineToolLocation", method = RequestMethod.GET)
    @Log(title = "新增机床库位" , businessType = BusinessType.INSERT)
    public ResponseBody addMachineToolLocation(@RequestParam("warehouseId") Integer warehouseId, @RequestParam("number") Integer number) {
        inventoryLocationService.addMachineToolLocation(warehouseId, number);
        return ResponseBody.success();
    }

    @RequestMapping(value = "/toolReturnIndex", method = RequestMethod.POST)
    public ResponseBody toolReturnIndex(@RequestBody ToolReturnIndexReq toolReturnIndexReq) {
        return ResponseBody.success(inventoryLocationService.toolReturnIndex(toolReturnIndexReq));
    }

    @RequestMapping(value = "/toolReturnIndexReload", method = RequestMethod.POST)
    public ResponseBody toolReturnIndexReload(@RequestBody ToolReturnIndexReq toolReturnIndexReq) {
        return ResponseBody.success(inventoryLocationService.toolReturnIndexReload(toolReturnIndexReq));
    }



    /**
     * 刀具分离归还
     *
     * @param toolReturnReq
     * @return
     */
    @RequestMapping(value = "/toolReturn", method = RequestMethod.POST)
    public ResponseBody toolReturn(@RequestBody ToolReturnReq toolReturnReq) {
        try {
            return ResponseBody.success(inventoryLocationService.toolReturn(toolReturnReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    @RequestMapping(value = "/qryAssemblyToolDetail", method = RequestMethod.GET)
    public ResponseBody qryAssemblyToolDetail(@RequestParam("uniqueId") String uniqueId) {
        return ResponseBody.success(inventoryLocationService.qryAssemblyToolDetail(uniqueId));
    }


    @RequestMapping(value = "/toolPartDamage", method = RequestMethod.POST)
    public ResponseBody toolPartDamage(@RequestBody ToolPartDamageReq toolPartDamageReq) {
        try {
            return ResponseBody.success(inventoryLocationService.toolPartDamage(toolPartDamageReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    @RequestMapping(value = "/toolPartReturn", method = RequestMethod.POST)
    public ResponseBody toolPartReturn(@RequestBody ToolPartReturnReq toolPartReturnReq) {
        try {
            return ResponseBody.success(inventoryLocationService.toolPartReturn(toolPartReturnReq));
        } catch (RuntimeException e) {
            log.error(e.getMessage());
            return ResponseBody.error("100002", "刀具归还异常");
        }
    }

    @RequestMapping(value = "/editLocationSafetyInventory", method = RequestMethod.POST)
    //@Log(title = "编辑库位安全库存" , businessType = BusinessType.UPDATE)
    public ResponseBody editLocationSafetyInventory(@RequestBody WarehouseSafetyInventoryParamReq warehouseSafetyInventoryParamReq) {
        return ResponseBody.success(inventoryLocationService.editLocationSafetyInventory(warehouseSafetyInventoryParamReq));
    }

    @RequestMapping(value = "/qryLocation", method = RequestMethod.GET)
    public ResponseBody qryLocation(@RequestParam("locationNum") String locationNum) {
        return ResponseBody.success(inventoryLocationService.qryLocation(locationNum));
    }


    @RequestMapping(value = "/backupAssemblyTool", method = RequestMethod.POST)
    public ResponseBody backupAssemblyTool(@RequestBody BackupAssemblyToolReq backupAssemblyToolReq) {
        try {
            return ResponseBody.success(inventoryLocationService.backupAssemblyTool(backupAssemblyToolReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    @RequestMapping(value = "/backupBatchTool", method = RequestMethod.POST)
    public ResponseBody backupBatchTool(@RequestBody BackupBatchToolReq backupBatchToolReq) {
        try {
            return ResponseBody.success(inventoryLocationService.backupBatchTool(backupBatchToolReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }



    @RequestMapping(value = "/backupUniqueIdAssemblyTool", method = RequestMethod.GET)
    public ResponseBody backupUniqueIdAssemblyTool(@RequestParam(value = "uniqueId" , required = false) String uniqueId,
                                                   @RequestParam(value = "id" , required = false) Integer id,
                                                   @RequestParam(value = "subProgramName" , required = false)String subprogramName,
                                                   @RequestParam(value = "toolLength" , required = false) String toolLength,
                                                   @RequestParam(value = "radius" , required = false) String radius,
                                                   @RequestParam(value = "operator" , required = false) String operator,
                                                   @RequestParam(value = "locationId" , required = false) Integer locationId) {
        try {
            return ResponseBody.success(inventoryLocationService.backupUniqueIdAssemblyTool(uniqueId , id , subprogramName , toolLength , radius , operator , locationId));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMsg());
        }
    }

    @RequestMapping(value = "/countNumberByLocationId", method = RequestMethod.GET)
    public ResponseBody countNumberByLocationId(@RequestParam("id") Integer id) {
        return ResponseBody.success(inventoryLocationService.countNumberByLocationId(id));
    }

    @RequestMapping(value = "/editParamByUniqueId", method = RequestMethod.GET)
    @Log(title = "编辑spu刀具参数" , businessType = BusinessType.UPDATE)
    public ResponseBody editParamByUniqueId(@RequestParam("radius") String radius ,
                                            @RequestParam("toolLength") String toolLength ,
                                            @RequestParam("life") String life,
                                            @RequestParam("uniqueId") String uniqueId) {
        return ResponseBody.success(inventoryLocationService.editParamByUniqueId(radius , toolLength ,life , uniqueId));
    }

    @RequestMapping(value = "/queryBackUpLocationId", method = RequestMethod.GET)
    public ResponseBody queryBackUpLocationId() {
        return ResponseBody.success(inventoryLocationService.queryBackUpLocationId());
    }

//    @RequestMapping(value = "/qryLocationSafetyInventory", method = RequestMethod.GET)
//    public ResponseBody qryLocationSafetyInventory(@RequestBody WarehouseSafetyInventoryParamReq warehouseSafetyInventoryParamReq) {
//        return ResponseBody.success(inventoryLocationService.editLocationSafetyInventory(warehouseSafetyInventoryParamReq));
//    }


//    @RequestMapping(value = "/toolReturnDetail", method = RequestMethod.GET)
//    public ResponseBody toolReturnDetail(@RequestParam("uniqueId") String uniqueId){
//        return ResponseBody.success(inventoryLocationService.toolReturnDetail(uniqueId));
//    }

    @RequestMapping(value = "/selectCountByToolType", method = RequestMethod.GET)
    public ResponseBody selectCountByToolType(@RequestParam("toolType") String toolType){
        return ResponseBody.success(inventoryLocationService.selectCountByToolType(toolType));
    }

    @RequestMapping(value = "/selectByRecord", method = RequestMethod.GET)
    public ResponseBody selectByRecord(@RequestParam("toolType") String toolType , @RequestParam(value = "id" , required = false)Integer id , @RequestParam(value = "groupId" , required = false)String groupId){
        return ResponseBody.success(inventoryLocationService.selectByRecordReload(toolType , id ,groupId));
    }

    @RequestMapping(value = "/selectByRecordReload", method = RequestMethod.GET)
    public ResponseBody selectByRecordReload(@RequestParam("toolType") String toolType , @RequestParam(value = "id" , required = false)Integer id , @RequestParam(value = "groupId" , required = false)String groupId){
        return ResponseBody.success(inventoryLocationService.selectByRecordReload1(toolType , id , groupId));
    }

    @RequestMapping(value = "/outboundUniqueTool", method = RequestMethod.POST)
    public ResponseBody outboundUniqueTool(@RequestBody InboundUniqueToolReq inboundToolReq){
        try {
            return ResponseBody.success(inventoryLocationService.outboundUniqueTool(inboundToolReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(),e.getMsg());
        }
    }

    @RequestMapping(value = "/selectUnSelectedList", method = RequestMethod.POST)
    public ResponseBody selectUnSelectedList(@RequestBody UnselectedReq unselectedReq){
        return ResponseBody.success(inventoryLocationService.selectUnSelectedList(unselectedReq));
    }

    @RequestMapping(value = "/updateUnselectedFlag", method = RequestMethod.GET)
    public ResponseBody updateUnselectedFlag(@RequestParam("id") String id , @RequestParam("flag")Integer flag){
        return ResponseBody.success(inventoryLocationService.updateUnselectedFlag(id , flag));
    }

    @RequestMapping(value = "/getWarehouseByToolNo", method = RequestMethod.GET)
    public ResponseBody getWarehouseByToolNo(@RequestParam("toolNo") String toolNo , @RequestParam("warehouseType") String warehouseType){
        return ResponseBody.success(inventoryLocationService.getWarehouseByToolNo(toolNo , warehouseType));
    }

    /**
     * 查询在组刀室未被选择的总成刀具
     * @param toolNo
     * @return
     */
    @RequestMapping(value = "/qryNnSelectedAssemblyTool", method = RequestMethod.GET)
    public ResponseBody qryNnSelectedAssemblyTool(@RequestParam("toolNo") String toolNo){
        return ResponseBody.success(inventoryLocationService.qryNnSelectedAssemblyTool(toolNo));
    }

    /**
     * 查询在组刀室未被选择的总成刀具
     * @return
     */
    @RequestMapping(value = "/qryEquipmentNameById", method = RequestMethod.GET)
    public ResponseBody qryEquipmentNameById(@RequestParam("id") Integer id){
        return ResponseBody.success(inventoryLocationService.qryEquipmentNameById(id));
    }

    /**
     * 修改库位使用容量
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseBody test(){
        return ResponseBody.success(inventoryLocationService.test());
    }

    /**
     * 导出货架excel模版
     * @param response
     */
    @RequestMapping(value = "/exportWarehouseTemplate", method = RequestMethod.GET)
    public void exportWarehouseTemplate(HttpServletResponse response) {
        try {
            inventoryLocationService.exportWarehouseTemplate(response);
        } catch (IOException e) {
            log.error("exportToolForkInfo error========》msg：{}" + e.getMessage());
        }
    }

    /**
     * 导入货架excel模版
     */
    @RequestMapping(value = "/importWarehouseTemplate", method = RequestMethod.POST)
    public ResponseBody<Boolean> importWarehouseTemplate(@RequestPart("file") MultipartFile file) {
        List<WarehouseImportEntity> importEntityList = new ArrayList<>();
        try {
            WarehouseImportListener warehouseImportListener = new WarehouseImportListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), WarehouseImportEntity.class, warehouseImportListener).extraRead(CellExtraTypeEnum.MERGE).headRowNumber(2).autoCloseStream(true).sheet().doRead();
            inventoryLocationService.importWarehouseTemplate(warehouseImportListener.getImportData());
            return ResponseBody.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("exportToolForkInfo error========》msg：{}" + e.getMessage());
            return ResponseBody.error("1001" , "解析错误");
        }
    }

    /**
     * 导出库位excel模版
     * @param response
     */
    @RequestMapping(value = "/exportInventoryLocationTemplate", method = RequestMethod.GET)
    public void exportInventoryLocationTemplate(HttpServletResponse response) {
        try {
            inventoryLocationService.exportInventoryLocationTemplate(response);
        } catch (IOException e) {
            log.error("exportInventoryLocation error========》msg：{}" + e.getMessage());
        }
    }

    /**
     * 导入货架excel模版
     */
    @RequestMapping(value = "/importLocationTemplate", method = RequestMethod.POST)
    public ResponseBody<Boolean> importLocationTemplate(@RequestPart("file") MultipartFile file) {
        List<WarehouseImportEntity> importEntityList = new ArrayList<>();
        try {
            LocationImportListener locationImportListener = new LocationImportListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), LocationImportEntity.class, locationImportListener).extraRead(CellExtraTypeEnum.MERGE).headRowNumber(2).autoCloseStream(true).sheet().doRead();
            inventoryLocationService.importLocationTemplate(locationImportListener.getImportData());
            return ResponseBody.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("exportToolForkInfo error========》msg：{}" + e.getMessage());
            return ResponseBody.error("1001" , "解析错误");
        }
    }

    /**
     * 解绑并重新绑定零件
     */
    @RequestMapping(value = "/unbindAndAssembleTool", method = RequestMethod.POST)
    @Transactional
    public ResponseBody unbindAndAssembleTool(@RequestBody UnbindAndAssembleToolReq unbindAndAssembleToolReq) {
        try {
            toolPartsInventoryService.unbindingAssemblyToolReload(unbindAndAssembleToolReq.getRfid() , unbindAndAssembleToolReq.getOriginalShankRfids() , unbindAndAssembleToolReq.getNewShankRfids(), unbindAndAssembleToolReq.getOriginalToolRfids() , unbindAndAssembleToolReq.getNewShankRfids() , unbindAndAssembleToolReq.getLocationId() , unbindAndAssembleToolReq.getOperator());
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
        try {
            return ResponseBody.success(inventoryLocationService.buildAssemblyTool(unbindAndAssembleToolReq.getNewToolRfids() , unbindAndAssembleToolReq.getNewShankRfids() , unbindAndAssembleToolReq.getOriginalToolNo() , unbindAndAssembleToolReq.getNewToolNo() , unbindAndAssembleToolReq.getOperator()));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode() , e.getMsg());
        }
    }

    /**
     * 获取总成分布图表
     */
    @RequestMapping(value = "/getWarehouseSum", method = RequestMethod.GET)
    public ResponseBody getWarehouseSum() {
            return ResponseBody.success(inventoryLocationService.getWarehouseSum());
    }

    /**
     * 获取总成报废图表
     */
    @RequestMapping(value = "/getToolDamageReport", method = RequestMethod.POST)
    public ResponseBody getToolDamageReport(@RequestBody ToolDamageReportReq toolDamageReportReq) {
        return ResponseBody.success(inventoryLocationService.getToolDamageReport(toolDamageReportReq));
    }

    /**
     * 获取总成报废图表首页接口
     */
    @RequestMapping(value = "/getToolDamageReportIndex", method = RequestMethod.POST)
    public ResponseBody getToolDamageReportIndex(@RequestBody ToolDamageReportReq toolDamageReportReq) {
        return ResponseBody.success(inventoryLocationService.getToolDamageReportIndex(toolDamageReportReq));
    }

    /**
     *
     */
    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public ResponseBody test1() {
        return ResponseBody.success(inventoryLocationService.test1());
    }

}

